package br.com.roteadroid.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.util.Log;

public class HttpTool {

	public HttpParams abreConexao(Boolean isProxy) {
		HttpParams httpParameters = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParameters, 600);
		HttpConnectionParams.setSoTimeout(httpParameters, 600);
		HttpConnectionParams.setTcpNoDelay(httpParameters, true);
		HttpConnectionParams.setConnectionTimeout(httpParameters, 7000);
		if(isProxy){
			HttpHost proxy = new HttpHost("10.230.22.33",  3128, "http");
			httpParameters.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		}
		
		return httpParameters;
	}
	
	public String executaPostJson(String enderecoServidor, String url, String json) {
	    HttpClient httpclient = new DefaultHttpClient(abreConexao(false));
	    Log.d("URL", url);
	   
	    StringBuilder builder = new StringBuilder();
	    try {
	    	URL urlT = new URL("http://" + enderecoServidor + url);
	    	URI uri = URIUtils.createURI(urlT.getProtocol(), urlT.getHost(), 80, url, null, null);
	 	    HttpPost httppost = new HttpPost(uri);
	 	    httppost.setHeader("Accept", "application/json");
	 	    httppost.setHeader("Content-type", "application/json");
	 	    StringEntity se = new StringEntity(json);
	        httppost.setEntity(se);

	        HttpResponse response = httpclient.execute(httppost);
	        StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) {
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
				}
			} else {
				//Erro
			}
	    } catch (ClientProtocolException e) {
	    	//Log.d("ClientProtocolException", e.getMessage());
	    } catch (IOException e) {
	    	e.printStackTrace();
	    	//Log.d("IOException", e.getMessage());
	    } catch (URISyntaxException e) {
	    	//Log.d("URISyntaxException", e.getMessage());
		}
	    
	    return builder.toString();
	}
	
	public String executaGet(String enderecoServidor, String url) {
		StringBuilder builder = new StringBuilder();
	       DefaultHttpClient httpclient = new DefaultHttpClient(abreConexao(false));
		try {
			URL urlT = new URL("http://" + enderecoServidor + url);
	    	URI uri = URIUtils.createURI(urlT.getProtocol(), urlT.getHost(), 80, url, "", "");
	    	HttpGet httpGet = new HttpGet(uri);
			HttpResponse response = httpclient.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) {
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
				}
			} else {
				//Erro
			}
	    } catch (ClientProtocolException e) {
	    	//Erro
	    } catch (IOException e) {
	    	//Erro
	    } catch (URISyntaxException e) {
	    	//Erro
		}
	    
		return builder.toString();
	}

	public String executaPost(String string, String string2,
			List<NameValuePair> preencheListaCampos) {
		// TODO Auto-generated method stub
		return null;
	}
	
}